//
//  WPNewMaleCaseCellTwo.m
//  wobingwoyi
//
//  Created by WGP on 16/11/3.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPNewMaleCaseCellTwo.h"

#define kHeightScale (self.contentView.frame.size.height/560)
#define kWidthScale (self.contentView.frame.size.width/375)
@interface WPNewMaleCaseCellTwo ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIImageView *bgImageView;
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,assign)CGFloat totalScale;

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *partArr;

@property(nonatomic,strong)UIButton *bodyBtn;
@property(nonatomic,strong)UIButton *heartBtn;
@property(nonatomic,strong)UIButton *skeletonBtn;
@property(nonatomic,strong)UIButton *currentBtn;
@property(nonatomic,strong)UIButton *changedBtn;

@property(nonatomic,assign)BOOL bodyPositive;//NO,正面,YES,方面
@property(nonatomic,assign)BOOL heartPosttive;
@property(nonatomic,assign)BOOL skeletonPositive;

@end
@implementation WPNewMaleCaseCellTwo

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.totalScale = 1.0;
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    [self scrollView];
//    [self bgImageView];
    [self imageView];
    
    [self setupBtns];
}

-(UIScrollView *)scrollView
{
    if (_scrollView == nil)
    {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.delegate = self;
        [_scrollView setMinimumZoomScale:1.0];
        [_scrollView setMaximumZoomScale:2.0f];
        _scrollView.bounces = NO;
        _scrollView.bouncesZoom = NO;
        
        CGSize newSize = [UIImage imageNamed:@"body"].size;
        //_scrollview可以拖动的范围
        [_scrollView setContentSize:newSize];
        [self.contentView addSubview:_scrollView];
        
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.edges.equalTo(self.contentView);
        }];
    }
    return _scrollView;
}

-(UIImageView *)bgImageView
{
    if (_bgImageView == nil)
    {
        _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bgCase"]];
        [self.scrollView addSubview:_bgImageView];
        
        [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
        
    }
    return _bgImageView;
}

-(UIImageView *)imageView
{
    if (_imageView == nil)
    {
        _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"body"]];
        _imageView.userInteractionEnabled = YES;
        [self.scrollView addSubview:_imageView];
//        _imageView.backgroundColor = [UIColor orangeColor];
         _imageView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
        // 单击
        UITapGestureRecognizer *SingleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resetImage:)];
        SingleTapGesture.numberOfTapsRequired = 1;//tap次数
        SingleTapGesture.numberOfTouchesRequired = 1;
        [_imageView addGestureRecognizer:SingleTapGesture];
        
        UITapGestureRecognizer* tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesAction:)];//给imageview添加tap手势
        tap.numberOfTapsRequired = 2;//双击图片执行tapGesAction
        _imageView.userInteractionEnabled=YES;
        [_imageView addGestureRecognizer:tap];
        
        // 关键在这一行，如果双击确定偵測失败才會触发单击
        [SingleTapGesture requireGestureRecognizerToFail:tap];
    }
    return _imageView;
}

-(void)setupBtns
{
    self.bodyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.bodyBtn setBackgroundImage:[UIImage imageNamed:@"body_icon"] forState:UIControlStateNormal];
    [self.bodyBtn addTarget:self action:@selector(partBtnDidClick:) forControlEvents:UIControlEventTouchDown];
    [self.contentView addSubview:self.bodyBtn];
    self.currentBtn = self.bodyBtn;
    
    [self.bodyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(30);
        make.top.equalTo(self.contentView).offset(50);
        make.width.height.mas_equalTo(35);
    }];
    
    self.heartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.heartBtn setBackgroundImage:[UIImage imageNamed:@"heart_icon"] forState:UIControlStateNormal];
    [self.heartBtn addTarget:self action:@selector(partBtnDidClick:) forControlEvents:UIControlEventTouchDown];
    [self.contentView addSubview:self.heartBtn];
    
    [self.heartBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.height.equalTo(self.bodyBtn);
        make.top.equalTo(self.bodyBtn.mas_bottom).offset(20);
    }];
    
    self.skeletonBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.skeletonBtn setBackgroundImage:[UIImage imageNamed:@"skeleton_icon"] forState:UIControlStateNormal] ;
    [self.skeletonBtn addTarget:self action:@selector(partBtnDidClick:) forControlEvents:UIControlEventTouchDown];
    [self.contentView addSubview:self.skeletonBtn];
    
    [self.skeletonBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.left.equalTo(self.bodyBtn);
        make.top.equalTo(self.heartBtn.mas_bottom).offset(20);
    }];
    
    self.changedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.changedBtn setBackgroundImage:[UIImage imageNamed:@"changeIcon"] forState:UIControlStateNormal];
    [self.changedBtn addTarget:self action:@selector(partBtnDidClick:) forControlEvents:UIControlEventTouchDown];
    [self.contentView addSubview:self.changedBtn];
    
    [self.changedBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(60);
        make.right.equalTo(self.contentView).offset(-30);
        make.bottom.equalTo(self.contentView).offset(-60);
    }];
}

-(void)partBtnDidClick:(UIButton *)btn
{
    if(self.currentBtn == btn)
    {
        return ;
    }
    
    if(btn == self.bodyBtn)
    {
        NSLog(@"body");
        self.currentBtn = self.bodyBtn;
        self.imageView.image = [UIImage imageNamed:@"body"];
    }
    else if (btn == self.heartBtn)
    {
        NSLog(@"heart");
        self.currentBtn = self.heartBtn;
        self.imageView.image = [UIImage imageNamed:@"bloodVessels"];
    }
    else if(btn == self.skeletonBtn)
    {
        NSLog(@"skeletonBtn");
        self.currentBtn = self.skeletonBtn;
        self.imageView.image = [UIImage imageNamed:@"skeleton"];
    }
    else if (btn == self.changedBtn)
    {
        //zoomScale这个值决定了contents当前扩展的比例
        if (self.scrollView.zoomScale > 1.0)
        {
            CGRect zoomRect = [self zoomRectForScale:1.0 withCenter:CGPointMake(10, 10)];
            [self.scrollView zoomToRect:zoomRect animated:YES];
        }
        
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
            if (self.currentBtn == self.bodyBtn)
            {
                
                [UIView transitionWithView:self.imageView duration:0.7 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
                    
                    if (!self.bodyPositive)
                    {
                        self.imageView.image = [UIImage imageNamed:@"body_Back"];
                    }
                    else
                    {
                        self.imageView.image = [UIImage imageNamed:@"body"];
                    }
                    self.bodyPositive = !self.bodyPositive;
                    
                } completion:^(BOOL finished) {
                    
                }];
            }
            else if(self.currentBtn == self.heartBtn)
            {
                
                [UIView transitionWithView:self.imageView duration:0.7 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
                    
                    self.imageView.image = [UIImage imageNamed:@"bloodVessels"];
                    
                } completion:^(BOOL finished) {
                    
                }];
            }
            else if (self.currentBtn == self.skeletonBtn)
            {
                
                [UIView transitionWithView:self.imageView duration:0.7 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
                    
                    if (!self.skeletonPositive)
                    {
                        self.imageView.image = [UIImage imageNamed:@"skeleton_Back"];
                    }
                    else
                    {
                        self.imageView.image = [UIImage imageNamed:@"skeleton"];
                    }
                    
                    self.skeletonPositive = !self.skeletonPositive;
                    
                } completion:^(BOOL finished) {
                    
                }];
            }

            
//        });
        
        }
}

//单击恢复视图
- (void)resetImage:(UITapGestureRecognizer *)recognizer
{
    CGPoint currentPoint = [recognizer locationInView:self.scrollView];
    CGPoint newCurrentPoint = CGPointMake(currentPoint.x/self.scrollView.zoomScale, currentPoint.y/self.scrollView.zoomScale);
    CGRect rect = CGRectMake(181*kWidthScale, 46*kHeightScale, 13, 15);
    
//    NSLog(@"%@********%@********%.2f",NSStringFromCGRect(rect),NSStringFromCGPoint(newCurrentPoint),self.scrollView.zoomScale);
    
    if (CGRectContainsPoint(rect, newCurrentPoint))
    {
        NSLog(@"点击了鼻子");
        [self bodyBtnDidClick];
    }
}

-(void)tapGesAction:(UIGestureRecognizer*)gestureRecognizer//手势执行事件
{
    
    float newScale = 0;
    //zoomScale这个值决定了contents当前扩展的比例
    if (self.scrollView.zoomScale > 1.0)
    {
        newScale = 1.0;
    }
    else
    {
        newScale = 2.0;
    }
    
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
    [self.scrollView zoomToRect:zoomRect animated:YES];
}

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center
{
    CGRect zoomRect;
    zoomRect.size.height = self.scrollView.frame.size.height / scale;
    zoomRect.size.width  = self.scrollView.frame.size.width  / scale;
    zoomRect.origin.x = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
    return zoomRect;
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

-(void)bodyBtnDidClick
{
    
    UIButton *coverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    self.coverBtn = coverBtn;
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    coverBtn.backgroundColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:0.5];
    [keyWindow addSubview:coverBtn];
    coverBtn.frame = [UIScreen mainScreen].bounds;
    [coverBtn addTarget:self action:@selector(coverBtnDidClick:) forControlEvents:UIControlEventTouchDown];
    //    if (btn == self.chestBtn)
    //    {
    self.tableView.frame = CGRectMake(self.contentView.center.x-kTableViewWidth*0.5, self.contentView.center.y-kTableViewHeight*0.5-100, kTableViewWidth, kTableViewHeight);
    [coverBtn addSubview:self.tableView];
}
-(UITableView *)tableView
{
    if (_tableView == nil)
    {
        _tableView = [[UITableView alloc] init];
        _tableView.layer.cornerRadius = 8;
        _tableView.layer.masksToBounds = YES;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.bounces = NO;
        _tableView.rowHeight = 45;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    }
    return _tableView;
}

-(NSArray *)partArr
{
    if (_partArr == nil)
    {
        _partArr = [NSArray arrayWithObjects:@"过敏性鼻炎",@"高血压脑病",@"痴呆",@"口腔溃疡",@"失眠", nil];
    }
    return _partArr;
}

-(void)coverBtnDidClick:(UIButton *)btn
{
    [btn removeFromSuperview];
    btn = nil;
}

#pragma mark - 数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.partArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    cell.textLabel.text = self.partArr[indexPath.item];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
@end
